﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySkiPatrolEntity;
using System.Data;
using MySkiPatrolSQL;

namespace MySkiPatrolDAL
{
    public class EventDAO
    {
    //can I get rid of this?
        SqlConnector SQLCONN = new SqlConnector();

        public Event UpdateEvent(Event calendarEvent)
        {
            if (calendarEvent.DataTransaction == null) calendarEvent.DataTransaction = new DataTransaction();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.UpdateEvent(calendarEvent), "temp");
                DataTable dt = ds.Tables["temp"];
                calendarEvent.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                calendarEvent.DataTransaction.Success = false;
                calendarEvent.DataTransaction.TransactionErrors = new List<Error>();
                calendarEvent.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }
            return calendarEvent;
        }

        public Event AddEvent(Event calendarEvent)
        {
            if (calendarEvent.DataTransaction == null) calendarEvent.DataTransaction = new DataTransaction();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.AddEvent(calendarEvent), "temp");
                DataTable dt = ds.Tables["temp"];
                calendarEvent.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                calendarEvent.DataTransaction.Success = false;
                calendarEvent.DataTransaction.TransactionErrors = new List<Error>();
                calendarEvent.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }
            return calendarEvent;
        }

        public Event DeleteEvent(Event calendarEvent)
        {
            if (calendarEvent.DataTransaction == null) calendarEvent.DataTransaction = new DataTransaction();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.DeleteEvent(calendarEvent), "temp");
                DataTable dt = ds.Tables["temp"];
                calendarEvent.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                calendarEvent.DataTransaction.Success = false;
                calendarEvent.DataTransaction.TransactionErrors = new List<Error>();
                calendarEvent.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }
            return calendarEvent;
        }

        public Event GetEvent(Event calendarEvent)
        {
            Event myReturn = new Event();
            if (myReturn.DataTransaction == null) myReturn.DataTransaction = new DataTransaction();

            if ((calendarEvent.ID != null) && (calendarEvent.ID != "0"))
            {
                try
                {
                    DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.GetEvent(calendarEvent), "temp");
                    DataTable dt = ds.Tables["temp"];

                    if (dt.Rows.Count == 1)
                    {
                        DataRow dr = dt.Rows[0];
                        myReturn.ID = DataConvertor.toString(dr, "eventid");
                        myReturn.OwnerID = DataConvertor.toString(dr, "memberid");
                        myReturn.StartDate = DataConvertor.toDateTime(dr, "startDate");
                        myReturn.EndDate = DataConvertor.toDateTime(dr, "endDate");
                        myReturn.AllDayEvent = DataConvertor.toBool(dr, "alldayevent");
                        myReturn.Title = DataConvertor.toString(dr, "EventTitle");
                        myReturn.Location = DataConvertor.toString(dr, "Location");
                        myReturn.Comments = DataConvertor.toString(dr, "Comments");
                        myReturn.OwnerName = DataConvertor.toString(dr, "ownername");
                        myReturn.VisibleOnRibbon = DataConvertor.toBool(dr, "visibleonribbon");
                        //myReturn.NumberOfSignupSlots = DataConvertor.toInt(dr, "NumberOfSignupSlots");
                        myReturn.IsPublic = DataConvertor.toBool(dr, "IsPublic");

                    }

                    //if (myReturn.NumberOfSignupSlots > 0)
                    //{
                    //    myReturn.Occurrences = this.GetEventRecurrence(myReturn.ID);
                    //}

                    myReturn.DataTransaction.Success = true;
                }
                catch (Exception ex)
                {
                    myReturn.DataTransaction.Success = false;
                    myReturn.DataTransaction.TransactionErrors = new List<Error>();
                    myReturn.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
                }
            }

            return myReturn;
        }

        //private List<EventRecurrence> GetEventRecurrence(string eventID)
        //{
        //    List<EventRecurrence> myReturn = new List<EventRecurrence>();

        //    try
        //    {
        //        DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.GetEventRecurrence(eventID), "temp");
        //        DataTable dt = ds.Tables["temp"];

        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            EventRecurrence item = new EventRecurrence();

        //            item.EventDate = DataConvertor.toDateTime(dr, "eventdate");
        //            item.EventID = DataConvertor.toString(dr, "eventid");
        //            item.EventRecurrenceID = DataConvertor.toString(dr, "eventrecurrenceid");
        //            item.NumberOfSignupSlots = DataConvertor.toInt(dr, "numberofsignupslots");
        //            item.Attendees = GetEventSignups(item);

        //            myReturn.Add(item);
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //    }

        //    return myReturn;
        //}

        //private List<EventSignup> GetEventSignups(EventRecurrence eventRecurrence)
        //{
        //    List<EventSignup> myReturn = new List<EventSignup>();

        //    try
        //    {
        //        DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.GetEventSignups(eventRecurrence), "temp");
        //        DataTable dt = ds.Tables["temp"];

        //        int slot = 0;
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            EventSignup item = new EventSignup();

        //            item.EventRecurrenceID = DataConvertor.toString(dr, "eventrecurrenceid");
        //            item.EventSignupID = DataConvertor.toString(dr, "eventsignupid");
        //            item.PersonID = DataConvertor.toString(dr, "personid");
        //            item.PersonName = DataConvertor.toString(dr, "personname");

        //            myReturn.Add(item);
        //            slot++;
        //        }

        //        for (int finsihSlot = 0; finsihSlot < eventRecurrence.NumberOfSignupSlots; finsihSlot++)
        //        {
        //            EventSignup item = new EventSignup();

        //            item.EventRecurrenceID = "";
        //            item.EventSignupID = "";
        //            item.PersonID = "";
        //            item.PersonName = "";

        //            myReturn.Add(item);
        //        }

        //    }
        //    catch (Exception ex)
        //    {

        //    }

        //    return myReturn;

        //}

        public EventCollection GetEvents(Event data)
        {
            EventCollection myReturn = new EventCollection();
            List<Event> myList = new List<Event>();
            if (myReturn.DataTransaction == null) myReturn.DataTransaction = new DataTransaction();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.GetEvents(data), "Event");
                DataTable dt = ds.Tables["Event"];

                foreach (DataRow dr in dt.Rows)
                {
                    Event item = new Event();
                    item.ID = DataConvertor.toString(dr, "eventid");
                    item.OwnerID = DataConvertor.toString(dr, "memberid");
                    item.StartDate = DataConvertor.toDateTime(dr, "startDate");
                    item.EndDate = DataConvertor.toDateTime(dr, "endDate");
                    item.AllDayEvent = DataConvertor.toBool(dr, "alldayevent");
                    item.Title = DataConvertor.toString(dr, "EventTitle");
                    item.Location = DataConvertor.toString(dr, "Location");
                    item.Comments = DataConvertor.toString(dr, "Comments");
                    item.VisibleOnRibbon = DataConvertor.toBool(dr, "visibleonribbon");
                    //item.NumberOfSignupSlots = DataConvertor.toInt(dr, "NumberOfSignupSlots");
                    item.IsPublic = DataConvertor.toBool(dr, "IsPublic");

                    myList.Add(item);
                }

                myReturn.Events = myList;
                myReturn.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                myReturn.DataTransaction.Success = false;
                myReturn.DataTransaction.TransactionErrors = new List<Error>();
                myReturn.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }

            return myReturn;
        }

        public EventCollection GetPublicEvents(Event data)
        {
            EventCollection myReturn = new EventCollection();
            List<Event> myList = new List<Event>();
            if (myReturn.DataTransaction == null) myReturn.DataTransaction = new DataTransaction();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(EventSQL.GetPublicEvents(data), "Event");
                DataTable dt = ds.Tables["Event"];

                foreach (DataRow dr in dt.Rows)
                {
                    Event item = new Event();
                    item.ID = DataConvertor.toString(dr, "eventid");
                    item.OwnerID = DataConvertor.toString(dr, "memberid");
                    item.StartDate = DataConvertor.toDateTime(dr, "startDate");
                    item.EndDate = DataConvertor.toDateTime(dr, "endDate");
                    item.AllDayEvent = DataConvertor.toBool(dr, "alldayevent");
                    item.Title = DataConvertor.toString(dr, "EventTitle");
                    item.Location = DataConvertor.toString(dr, "Location");
                    item.Comments = DataConvertor.toString(dr, "Comments");
                    item.VisibleOnRibbon = DataConvertor.toBool(dr, "visibleonribbon");
                    //item.NumberOfSignupSlots = DataConvertor.toInt(dr, "NumberOfSignupSlots");
                    item.IsPublic = DataConvertor.toBool(dr, "IsPublic");

                    myList.Add(item);
                }

                myReturn.Events = myList;
                myReturn.DataTransaction.Success = true;
            }
            catch (Exception ex)
            {
                myReturn.DataTransaction.Success = false;
                myReturn.DataTransaction.TransactionErrors = new List<Error>();
                myReturn.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }

            return myReturn;
        }


    }
}